package entity.lemming;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import entity.Entity;
import entity.terrain.Terrain;
import level.Level;

/**
 * A Bomber Lemming, a Lemming that self destructs after 5 seconds.
 * 
 * @author Neil Cowzer
 */
public class Bomber extends Lemming {
	
	/** countdown till self-destruct */
	private int timer = 500;
	
	/** the current count */
	private int count;
	
	/**
	 * Create an instance of the Bomber Lemming.
	 * 
	 * @param x the starting x-coordinate.
	 * @param y the starting y-coordinate.
	 */
	/*@ requires 0 <= x && 0 <= y;
		assignable speed_X && speed_Y && energy;
		ensures speed_X != 0 && energy == 100;
	 */
	public Bomber(int x, int y){
		super("resource/graphics/lemming/walker/walker1.gif",8, x, y);
		speed_X = 0;
		speed_Y = 0;
		energy = 100;
	}
	
	/**
	 * Checks for a collision with another Entity
	 * 
	 * @param other another Entity eg Terrain, Lemming.
	 * @return true if a collision has occured.
	 */
	public /*@ pure @*/ boolean collision(Entity other) {
		// TODO Auto-generated method stub
		return false;
	}

	/** Request that this lemming perform the appropiate action/movement. */
	/*@ ensures x == \old(x) + speed_X &&
				energy == \old(energy) - 1;
	 */
	public void move(Terrain terrain) {
		
		if (count == timer/5){
			// only display the shaking head animation, no movement
			generateFrames("tobefilled", 7);
		}
		else if (count <= timer/5){
			// no motion, just animation
			update();
		}
		else {
			// he is essentially a walker
			// cut an paste walker move below
			// calculate the next y coordinate
			int new_y = y;
			Rectangle lemming = getBounds();
			
			if (lemming.intersects(terrain.getBounds())){
				BufferedImage image = ((Terrain)terrain).getImage().getBufferedImage();
					int black = 16777215; // black that represents the background
					
					try{
						
						for (int curr_y = lemming.y; curr_y < lemming.y + lemming.height + (lemming.height/2); curr_y++){
							int color = 0;
							
							
								if (speed_X >= 0){
									color = image.getRGB(x + lemming.width/2 + 3, curr_y);
								}
								else {
									color = image.getRGB(x + lemming.width/2 - 3, curr_y);
								}
							
								// if we have a pixel to stand on
								if (color != black){
									if(curr_y < lemming.y + lemming.height/2){
											turnAround();
									}
									else{
										new_y = curr_y - lemming.width -1;
									}
									break;
								}
								else{
									// Gaurd! not allowed fall unless all the "ground" beneath him is black
									boolean onlyBlack = true;
									while (onlyBlack){
										for (int fx = lemming.x; fx<lemming.x+lemming.width; fx++){
											for (int fy = lemming.y+lemming.height; fy <lemming.y+lemming.height+lemming.height/2; fy++){
												int testcolor =  image.getRGB(fx, fy);
												//System.out.println(testcolor);
												if (testcolor != black){
													onlyBlack = false;
												}
											}
										}
										break;
									}
									if (onlyBlack){
										fall = true;
									}
									
								}
						}
					}catch(ArrayIndexOutOfBoundsException e){
						
					}
			}
			
			// always perform a move in the x direction
			x= x+speed_X;
			y = new_y;	
			update();
		}

	}
	
	/** Reverse a Lemmings direction */
	//@ ensures speed_Y == 0 - \old(speed_Y);
	public void turnAround(){
		speed_X = -(speed_X);
	}

}
